import Vue from "vue";
import VueRouter from "vue-router";
import Layout from "../views/layout/index.vue";
import Login from "../views/layout/login.vue";
import notFound from "../views/layout/404.vue";

Vue.use(VueRouter);

const routes = [
	{
		path: '/login',
		component: Login,
		// 是否在右侧导航栏显示
		hidden: true
	},
	{
		path: '*',
		component: notFound,
		// 是否在右侧导航栏显示
		hidden: true
	},
	// 只有一级菜单的路由
	{
		path: '/',
		name: 'Home',
		component: Layout,
		meta: { title: '用户管理' },
		children: [{
			path: "/",
			name: "user",
			component: () => import("../views/admin/admin.vue"),
			meta: {
				title: "个人中心"
			}
		},
		]
	},
	// 有二级菜单的路由
	{
		path: "/user",
		component: Layout,
		name: "user",
		meta: { title: "用户管理" },
		children: [
			{
				path: "/insert",
				name: "insertUser",
				component: () => import("../views/admin/insert.vue"),
				meta: { title: "新增用户" },
				// 是否在右侧导航栏显示
				hidden: true
			},
			{
				path: "/list",
				name: "list",
				component: () => import("../views/admin/user.vue"),
				meta: { title: "用户列表" },
			},
		],
	},
	// 只有一级菜单的路由
	{
		path: '/echarts',
		name: 'echarts',
		component: Layout,
		meta: { title: '折线图' },
		children: [{
			path: "/",
			name: "echarts",
			component: () => import("../views/admin/echarts.vue"),
			meta: {
				title: "折线图"
			}
		},
		]
	},
];

const router = new VueRouter({
	routes,
});
router.beforeEach((to, from, next) => {
	// to：即将要进入的目标 路由对象（现在的页面）
	// from：当前导航正要离开的路由（上一步的页面）
	// next：
	if (localStorage.getItem('userInfo')) {
		next();
	} else {
		if (to.path === '/login') {
			next();
		} else {
			next('/login');
		}
	}
})
export default router;
